<?
    class Principio_Model_UserMapper extends Principio_Mapper
    {
        protected static $db_table = null;
        protected static function getDbTableName() { return "User"; }


        private static function _getAuthAdapter($login, $password)
        {
            $auth_adapter = new Zend_Auth_Adapter_DbTable(
                Zend_Db_Table::getDefaultAdapter(),
                'user',
                'login',
                'password'
            );
            $auth_adapter->setIdentity($login)->setCredential($password);

            return $auth_adapter;
        }

        public static function authorize($login, $password)
        {
            $auth = Zend_Auth::getInstance();
            $model_user = new Principio_Model_User();
            $model_user->setLogin($login)->setPassword($password);
            $auth_adapter = self::_getAuthAdapter($model_user->getLogin(), $model_user->getPassword());
            $result = $auth->authenticate($auth_adapter);
            if ($result->isValid())
            {
                $auth_obj = $auth_adapter->getResultRowObject();
                $model_user->setID($auth_obj->id)->setName($auth_obj->name)->setLastName($auth_obj->last_name)->setLastLogin(date("Y-m-d H:i:s"))->setRole($auth_obj->role)->setRegisterDate($auth_obj->register_date);
                self::updateLastLoginDate($model_user);
                $auth->getStorage()->write(array(
                    'user_id' => $model_user->getID()
                ));
                return $model_user;
            }
            else
                return false;
        }


        public static function updateLastLoginDate(Principio_Model_User $model_user)
        {
            return self::getDBTable()->update(['last_login' => $model_user->getLastLogin()], ['id=?' => $model_user->getID()]);
        }


        public static function fetch($id)
        {
            try
            {
                $user_row = self::getDBTable()->find(intval($id));
                return (is_null($user_row))? false : new Principio_Model_User($user_row[0]->toArray());
            }
            catch(Exception $ex)
            {
                return self::reportException($ex);
            }
        }


        public static function fetchAll()
        {
            try
            {
                $values = self::getDBTable()->fetchAll();
                return Principio_Model_User::makeCollection($values->toArray());
            }
            catch(Exception $ex)
            {
                return self::reportException($ex);
            }
        }


        public static function checkUniqueLogin(Principio_Model_User $model_user)
        {
            try
            {
                $select = self::getDBTable()->select(Zend_Db_Table_Abstract::SELECT_WITH_FROM_PART)->where('login=?', $model_user->getLogin())->where('id<>?', $model_user->getID())->columns("id");
                return (is_null(self::getDBTable()->fetchRow($select)));
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function save(Principio_Model_User $model_user)
        {
            try
            {
                $data = [
                    'login' => $model_user->getLogin(),
                    'role' => $model_user->getRole(),
                    'name' => $model_user->getName(),
                    'last_name' => $model_user->getLastName(),
                    'register_date' => $model_user->getRegisterDate(),
                    'last_login' => $model_user->getLastLogin()
                ];

                if ($model_user->getPassword() != "")
                    $data['password'] = $model_user->getPassword();

                if ($model_user->getID() == -1)
                {
                    $model_user->setID(self::getDBTable()->insert($data));
                    return true;
                }
                else
                    return self::getDBTable()->update($data, ['id=?' => $model_user->getID()]);

            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            }
        }


        public static function delete(Principio_Model_User $model_user)
        {
            try
            {
                self::getDBTable()->delete(['id=?' => $model_user->getID()]);
                return true;
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }

    }
?>